package com.hedl.practice.diyJava.network_programming.j_email;

import com.hedl.practice.diyJava.network_programming.j_email.utils.SendEmail;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 使用JavaMail API发送邮件本质上是一个MUA软件通过SMTP协议发送邮件至MTA服务器；
 * 打开调试模式可以看到详细的SMTP交互信息；
 * 某些邮件服务商需要开启SMTP，并需要独立的SMTP登录密码。
 */
public class Client {
    public static void main(String[] args) throws Exception {
        final String smtp = "smtp.qq.com";
        final String username = "302572985@qq.com";
        final String password = "snyfqsyfdqnkcadg";
        final String from = "302572985@qq.com";
        final String to = "3356428450@qq.com";

        //html和正常文本格式
        /*SendEmail sender = new SendEmail(smtp,username,password);
        Session session = sender.createTLSSession();
        Message message = sender.createTestMessage(session,from,to,"JavaMail邮件", "<h1>Hello</h1><p>这是来自www.baidu.com 的邮件");
        //Message message = sender.createTestMessageHtml(session,from,to,"JavaQQ邮件","<h1>Hello</h1><p>这是来自www.baidu.com 的邮件</p>");
        Transport.send(message);*/

        //发送附件试试
        /*SendEmail sender = new SendEmail(smtp,username,password);
        Session session = sender.createTLSSession();
        try (InputStream input = new FileInputStream("/Users/a101/Java-Project/study/Daily-Learning/src/main/resources/image/耳朵.jpg")){
            Message message = sender.createMessageWithAttachment(session,from,to,"Hello Java邮件带附件","<h1>Hello</h1><p>这是一封带附件的<u>javamail</u>邮件！</p>","耳朵.jpg",input);
            Transport.send(message);
        }*/

        //发送内嵌图片的HTML邮件
        /**
         * 如果给一个<img src="http://example.com/test.jpg">，
         * 这样的外部图片链接通常会被邮件客户端过滤，并提示用户显示图片并不安全。只有内嵌的图片才能正常在邮件中显示。
         */
        SendEmail sender = new SendEmail(smtp,username,password);
        Session session = sender.createTLSSession();
        try(InputStream input = new FileInputStream("/Users/a101/Java-Project/study/Daily-Learning/src/main/resources/image/耳朵.jpg")){
            Message message = sender.createMessageWithInlineImage(session,from,to,"Hello Java HTML邮件内嵌图片","<h1>Hello</h1><p><img src=\"cid:img01\"></p><p>这是一封内嵌图片的<u>javamail</u>邮件！</p>","耳朵.jpg",input);
            Transport.send(message);        //提交发送
        }

    }
}
